<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
          v-hasPerm="['system:product-sales:add']"
          type="success"
          @click="handleOpenDialog()"
        >
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button
          v-hasPerm="['system:product-sales:delete']"
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          key="id"
          label="主键"
          prop="id"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="timePeriod"
          label="时间周期"
          prop="timePeriod"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="productName"
          label="商品名称"
          prop="productName"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="sku"
          label="SKU"
          prop="sku"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="brand"
          label="品牌"
          prop="brand"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="firstCategory"
          label="一级类别"
          prop="firstCategory"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="secondCategory"
          label="二级类别"
          prop="secondCategory"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="thirdCategory"
          label="三级类别"
          prop="thirdCategory"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="shopShortName"
          label="店铺简称"
          prop="shopShortName"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="operationMode"
          label="经营模式"
          prop="operationMode"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="pageViews"
          label="浏览量"
          prop="pageViews"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="visitorCount"
          label="访客数"
          prop="visitorCount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="avgPageViewsPerVisitor"
          label="人均浏览量"
          prop="avgPageViewsPerVisitor"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="avgStayTime"
          label="平均停留时长"
          prop="avgStayTime"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="transactionUserCount"
          label="成交人数"
          prop="transactionUserCount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="conversionRate"
          label="成交转化率"
          prop="conversionRate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="transactionOrderCount"
          label="成交单量"
          prop="transactionOrderCount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="transactionItemCount"
          label="成交商品件数"
          prop="transactionItemCount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="transactionAmount"
          label="成交金额"
          prop="transactionAmount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="averageTransactionValue"
          label="成交客单价"
          prop="averageTransactionValue"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="createTime"
          label="创建时间"
          prop="createTime"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="updateTime"
          label="更新时间"
          prop="updateTime"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="isDeleted"
          label="删除标记(0:正常 1:删除)"
          prop="isDeleted"
          min-width="150"
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              v-hasPerm="['system:product-sales:edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon><Edit /></template>
              编辑
            </el-button>
            <el-button
              v-hasPerm="['system:product-sales:delete']"
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 商品销售数据表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="主键" prop="id">
          <el-input
            v-model="formData.id"
            placeholder="主键"
          />
        </el-form-item>

        <el-form-item label="时间周期" prop="timePeriod">
          <el-input
            v-model="formData.timePeriod"
            placeholder="时间周期"
          />
        </el-form-item>

        <el-form-item label="商品名称" prop="productName">
          <el-input
            v-model="formData.productName"
            placeholder="商品名称"
          />
        </el-form-item>

        <el-form-item label="SKU" prop="sku">
          <el-input
            v-model="formData.sku"
            placeholder="SKU"
          />
        </el-form-item>

        <el-form-item label="品牌" prop="brand">
          <el-input
            v-model="formData.brand"
            placeholder="品牌"
          />
        </el-form-item>

        <el-form-item label="一级类别" prop="firstCategory">
          <el-input
            v-model="formData.firstCategory"
            placeholder="一级类别"
          />
        </el-form-item>

        <el-form-item label="二级类别" prop="secondCategory">
          <el-input
            v-model="formData.secondCategory"
            placeholder="二级类别"
          />
        </el-form-item>

        <el-form-item label="三级类别" prop="thirdCategory">
          <el-input
            v-model="formData.thirdCategory"
            placeholder="三级类别"
          />
        </el-form-item>

        <el-form-item label="店铺简称" prop="shopShortName">
          <el-input
            v-model="formData.shopShortName"
            placeholder="店铺简称"
          />
        </el-form-item>

        <el-form-item label="经营模式" prop="operationMode">
          <el-input
            v-model="formData.operationMode"
            placeholder="经营模式"
          />
        </el-form-item>

        <el-form-item label="浏览量" prop="pageViews">
          <el-input
            v-model="formData.pageViews"
            placeholder="浏览量"
          />
        </el-form-item>

        <el-form-item label="访客数" prop="visitorCount">
          <el-input
            v-model="formData.visitorCount"
            placeholder="访客数"
          />
        </el-form-item>

        <el-form-item label="人均浏览量" prop="avgPageViewsPerVisitor">
          <el-input
            v-model="formData.avgPageViewsPerVisitor"
            placeholder="人均浏览量"
          />
        </el-form-item>

        <el-form-item label="平均停留时长" prop="avgStayTime">
          <el-input
            v-model="formData.avgStayTime"
            placeholder="平均停留时长"
          />
        </el-form-item>

        <el-form-item label="成交人数" prop="transactionUserCount">
          <el-input
            v-model="formData.transactionUserCount"
            placeholder="成交人数"
          />
        </el-form-item>

        <el-form-item label="成交转化率" prop="conversionRate">
          <el-input
            v-model="formData.conversionRate"
            placeholder="成交转化率"
          />
        </el-form-item>

        <el-form-item label="成交单量" prop="transactionOrderCount">
          <el-input
            v-model="formData.transactionOrderCount"
            placeholder="成交单量"
          />
        </el-form-item>

        <el-form-item label="成交商品件数" prop="transactionItemCount">
          <el-input
            v-model="formData.transactionItemCount"
            placeholder="成交商品件数"
          />
        </el-form-item>

        <el-form-item label="成交金额" prop="transactionAmount">
          <el-input
            v-model="formData.transactionAmount"
            placeholder="成交金额"
          />
        </el-form-item>

        <el-form-item label="成交客单价" prop="averageTransactionValue">
          <el-input
            v-model="formData.averageTransactionValue"
            placeholder="成交客单价"
          />
        </el-form-item>

        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formData.createTime"
            type="datetime"
            placeholder="创建时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item label="更新时间" prop="updateTime">
          <el-date-picker
            v-model="formData.updateTime"
            type="datetime"
            placeholder="更新时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item label="删除标记(0:正常 1:删除)" prop="isDeleted">
          <el-input
            v-model="formData.isDeleted"
            placeholder="删除标记(0:正常 1:删除)"
          />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "ProductSales",
  inheritAttrs: false,
});

import ProductSalesAPI, { ProductSalesPageVO, ProductSalesForm, ProductSalesPageQuery } from "@/api/system/product-sales.api";

const queryFormRef = ref();
const dataFormRef = ref();

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<ProductSalesPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 商品销售数据表格数据
const pageData = ref<ProductSalesPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 商品销售数据表单数据
const formData = reactive<ProductSalesForm>({});

// 商品销售数据表单校验规则
const rules = reactive({
  id: [{ required: true, message: "请输入主键", trigger: "blur" }],
  timePeriod: [{ required: true, message: "请输入时间周期", trigger: "blur" }],
  productName: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
  sku: [{ required: true, message: "请输入SKU", trigger: "blur" }],
  brand: [{ required: true, message: "请输入品牌", trigger: "blur" }],
  firstCategory: [{ required: true, message: "请输入一级类别", trigger: "blur" }],
  secondCategory: [{ required: true, message: "请输入二级类别", trigger: "blur" }],
  thirdCategory: [{ required: true, message: "请输入三级类别", trigger: "blur" }],
  shopShortName: [{ required: true, message: "请输入店铺简称", trigger: "blur" }],
  operationMode: [{ required: true, message: "请输入经营模式", trigger: "blur" }],
  pageViews: [{ required: true, message: "请输入浏览量", trigger: "blur" }],
  visitorCount: [{ required: true, message: "请输入访客数", trigger: "blur" }],
  avgPageViewsPerVisitor: [{ required: true, message: "请输入人均浏览量", trigger: "blur" }],
  avgStayTime: [{ required: true, message: "请输入平均停留时长", trigger: "blur" }],
  transactionUserCount: [{ required: true, message: "请输入成交人数", trigger: "blur" }],
  conversionRate: [{ required: true, message: "请输入成交转化率", trigger: "blur" }],
  transactionOrderCount: [{ required: true, message: "请输入成交单量", trigger: "blur" }],
  transactionItemCount: [{ required: true, message: "请输入成交商品件数", trigger: "blur" }],
  transactionAmount: [{ required: true, message: "请输入成交金额", trigger: "blur" }],
  averageTransactionValue: [{ required: true, message: "请输入成交客单价", trigger: "blur" }],
  createTime: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  updateTime: [{ required: true, message: "请输入更新时间", trigger: "blur" }],
  isDeleted: [{ required: true, message: "请输入删除标记(0:正常 1:删除)", trigger: "blur" }],
});

/** 查询商品销售数据 */
function handleQuery() {
  loading.value = true;
  ProductSalesAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置商品销售数据查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开商品销售数据弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改商品销售数据";
    ProductSalesAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增商品销售数据";
  }
}

/** 提交商品销售数据表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        ProductSalesAPI.update(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        ProductSalesAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭商品销售数据弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除商品销售数据 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      ProductSalesAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
